/**
 * Created by shadow on 2017/10/19.
 */
var dataGridId = "dataList";
$(function () {
    var url = basePath + "/flow/flow-running-query";
    $('#dataList').datagrid('options').url = url;
    $("#dataList").datagrid("reload")
    //$("#dataList").datagrid("loadData", data);
    resizeDataGridSize(dataGridId);
    autoResizeDataGrid(dataGridId);
});

/**
 * 删除流程实例
 */
function deleteInstance() {
    var row = $('#dataList').datagrid('getSelected');
    if (row) {
        var processInstanceId = row.processInstanceId;
        var url = basePath + "/process/" + processInstanceId + "/deleteProcessInstanceByID";
        layer.confirm('确认删除？', {icon: 3, title: '提示'}, function (index) {
            $.ajax({
                type: "POST",
                url: url,
                data: {},
                dataType: "json",
                success: function (data) {
                    layer.alert("删除成功", {closeBtn: 0, shift: 4});
                    $("#dataList").datagrid("reload");
                    layer.close(index);
                },
                error: function (data, textstatus) {
                    $("#dataList").datagrid("reload");
                    layer.alert("删除失败", {closeBtn: 0, shift: 4});
                    layer.close(index);
                }
            });

        });


    } else {
        layer.alert("请选择", {closeBtn: 0, shift: 4});
    }
}




/**
 * 撤回任务
 */
function revokeTask() {
    var row = $('#dataList').datagrid('getSelected');
    if (row) {
        var processInstanceId = row.processInstanceId;
        var url = basePath + "/task/" + processInstanceId + "/zhuyongjie/revokeTask";
        layer.confirm('确认撤回？', {icon: 3, title: '提示'}, function (index) {
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify({}),
               // dataType: "json",
                contentType: 'application/json',
                success: function (data) {
                    layer.alert(data, {closeBtn: 0, shift: 4});
                    $("#dataList").datagrid("reload");
                    layer.close(index);
                },
                error: function (data, textstatus) {
                    $("#dataList").datagrid("reload");
                    layer.alert("删除失败", {closeBtn: 0, shift: 4});
                    layer.close(index);
                }
            });

        });


    } else {
        layer.alert("请选择", {closeBtn: 0, shift: 4});
    }
}

/**
 * 办理流程
 */
function dealMatter() {
    var row = $('#dataList').datagrid('getSelected');
    if (row) {
        var processInstanceId = row.processInstanceId;
        var userId = "zhuyongjie";
        var url = basePath + "/task/" + processInstanceId + "/" + userId + "/cliamAndCompleteTaskByInstanceId";
        var activityId = row.activityId;

        $.ajax({
            type: "POST",
            url: url,
            data: JSON.stringify(dealData[activityId]),
            //  dataType: "json",
            contentType: 'application/json',
            success: function (data) {
                layer.alert("办理成功", {closeBtn: 0, shift: 4});
                $("#dataList").datagrid("reload");
            },
            error: function (data, textstatus) {
                $("#dataList").datagrid("reload");
                layer.alert("办理失败", {closeBtn: 0, shift: 4});
            }
        });


    } else {
        layer.alert("请选择", {closeBtn: 0, shift: 4});
    }
}

var dealData = {
    "usertask1": {
        "audit2": "huangdongdong"
    },
    "usertask2": {
        "auditFlag": "true"
    }
};


/**
 * 查看流程图
 */
function viewPic() {
    var row = $('#dataList').datagrid('getSelected');
    if (row) {
        var processInstanceId = row.processInstanceId;
        var processDefinitionId = row.processDefinitionId;

        var url = basePath + '/common-web/resource/diagram-viewer/index.html?processDefinitionId=' + processDefinitionId + '&processInstanceId=' + processInstanceId+"&randomId="+Math.random();

        var clientWidth = document.body.clientWidth * 0.9 + 'px';
        var clientHeight = document.body.clientHeight + 'px';
        parent.layer.open({
            title: "流程图",
            type: 2,
            area: [clientWidth, clientHeight],
            content: url,
            maxmin: true
        })

    } else {
        layer.alert("请选择", {closeBtn: 0, shift: 4});
    }
}

function viewPic2() {
    var row = $('#dataList').datagrid('getSelected');
    if (row) {
        var processInstanceId = row.processInstanceId;
        var processDefinitionId = row.processDefinitionId;
        showDiagramViewer({
            "processInstanceId": processInstanceId,
            "processDefinitionId": processDefinitionId
        })

    } else {
        layer.alert("请选择", {closeBtn: 0, shift: 4});
    }
}
